package processes.nMCounterProcess;

import os.MiniOS;
import os.MiniProcess;
import os.MiniThread;

/**
 * THe collector thread collects value maintained by the {@link MMonitorThread}
 */
public class MCollectorThread extends MiniThread
{
	/**
	 * @see Buffer
	 */
	private Buffer buffer;

	/**
	 * Creates {@link MCollectorThread}
	 * @param process
	 * @param tid
	 */
	public MCollectorThread(MiniProcess process, String tid)
	{
		super(process, tid);
		description = "NMCounter thread";
	}

	@Override
	public void passThreadArguments(Object[] arguments)
	{
		buffer = (Buffer) arguments[0];
	}

	@Override
	public void run()
	{
		while (! buffer.shouldTerminateCollector())
		{
			isExcecutionStopped();
			int result = buffer.read();
			isExcecutionStopped();
			MiniOS.getInstance().outputRequest(this, "Number counted = " + result);
		}
		isExcecutionStopped();
		MiniOS.getInstance().terminateThread(this);
	}
}
